package cn.breeze.pet.controller;

import cn.breeze.basic.annotation.PreAuthorize;
import cn.breeze.basic.exception.BusinessException;
import cn.breeze.basic.jwt.UserInfo;
import cn.breeze.basic.utils.AjaxResult;
import cn.breeze.basic.utils.LoginContext;
import cn.breeze.basic.utils.PageList;
import cn.breeze.org.domain.Employee;
import cn.breeze.org.service.IEmployeeService;
import cn.breeze.pet.domain.Pet;
import cn.breeze.pet.query.PetQuery;
import cn.breeze.pet.service.IPetService;
import cn.breeze.user.domain.Logininfo;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private  IPetService petService;
    @Autowired
    private IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存pet", sn = "pet:save")
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest req){
        try {
            UserInfo loginUser = (UserInfo)LoginContext.getLoginUser(req);
            Logininfo logininfo = loginUser.getLogininfo();
            Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
            pet.setShopId(employee.getShopId());
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.save(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除pet", sn = "pet:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取pet", sn = "pet:get")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "获取所有pet", sn = "pet:loadAll")
    public List<Pet> list(){

        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "列表pet", sn = "pet:list")
    public PageList<Pet> queryData(@RequestBody PetQuery query)
    {
        return petService.queryData(query);
    }

    /**
    * @Title: onsale
    * @Description: 商品上架
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/16 23:53
    * @Parameters: [ids]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/onsale")
    /*@PreAuthorize(name = "删除pet", sn = "pet:onsale")*/
    public AjaxResult onsale(@RequestBody Long[] ids){
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }

    /**
    * @Title: offsale
    * @Description: 商品下架
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/16 23:55
    * @Parameters: [ids]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/offsale")
    /*@PreAuthorize(name = "删除pet", sn = "pet:onsale")*/
    public AjaxResult offsale(@RequestBody Long[] ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }

    /**
    * @Title: patchRemove
    * @Description: 批量删除
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/18 16:57
    * @Parameters: [ids]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PatchMapping
    /*@PreAuthorize(name = "批量删除宠物", sn = "pet:patchDelete")*/
    public AjaxResult patchRemove(@RequestBody Long[] ids){
        try {
            petService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试试！");
        }
    }
}
